import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:search_people/utils/component/component.dart';
import 'package:search_people/utils/component/list/table_view.dart';
import 'package:search_people/utils/util.dart';
import 'package:search_people/value/topcolors.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class DynamicSearchPage extends GetView<DynamicSearchController> {
  const DynamicSearchPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    return Column(
      children: [
        state.isSearching
            ? const SizedBox()
            : SearchHistoryView(
                list: state.his,
              ),
        Expanded(
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(2.r),
            ),
            margin: EdgeInsets.all(12.r),
            child: TableView(
              data:
                  state.isSearching ? state.results : controller.state.initList,
              separatorIndent: EdgeInsets.only(left: 39.r, right: 12.r),
              sepratorColor: TopColors.bg,
              hasSeprator: !state.isSearching,
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DynamicSearchController>(
      builder: (_) {
        return Scaffold(
          appBar: detailAppBar(context,
              leadding: Padding(
                padding: EdgeInsets.only(left: 12.r, top: 6.r, bottom: 6.r),
                child: searchBar(
                  controller: controller.state.tc,
                  onSubmitted: controller.onSubmmit,
                  onChanged: (_) {
                    controller.onInputChanged();
                  },
                  hintText: '请输入关键词',
                  backgroundColor: Colors.white,
                  borderRadius: BorderRadius.circular(5),
                  borderColor: TopColors.main_color,
                  hintStyle: TextStyle(
                    color: TopColors.black_dd,
                    fontSize: 14.sp,
                  ),
                  labelStyle: TextStyle(
                    color: TopColors.black_33,
                    fontSize: 14.sp,
                  ),
                  iconColor: TopColors.black_dd,
                ),
              ),
              leadingWidth: Get.width - 60.r,
              actions: [
                TextButton(
                    onPressed: Get.back,
                    child: Text(
                      '取消',
                      style: TextStyle(
                        color: TopColors.black_33,
                        fontSize: 15.sp,
                      ),
                    ))
              ]),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
